from flask import Blueprint, render_template, request, session
from flask_login import login_required, current_user
from sqlalchemy import desc
from applications.common.curd import model_to_dicts, enable_status, disable_status, get_one_by_id
from applications.common import curd
from applications.common.curd import enable_status, disable_status
from applications.common.utils.http import table_api, fail_api, success_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import str_escape
from applications.extensions import db
from applications.models import Student


admin_student = Blueprint('adminStudent', __name__, url_prefix='/admin/student')


# 用户管理
@admin_student.get('/')
@authorize("admin:student:main")
def main():

    return render_template('admin/student/index.html')


# 表格数据
@admin_student.get('/data')
@authorize("admin:student:main")
def table():
    name = str_escape(request.args.get('realName', type=str))
    print(name)
    # print(role_name,"fffffffffffffffffffffffff")
    # role_code = str_escape(request.args.get('roleCode', type=str))
    filters = []
    if name:
        filters.append(Student.name.contains(name))
    # if role_code:
    #     filters.append(Role.code.contains(role_code))
    roles = Student.query.filter(*filters).layui_paginate()
    # return table_api(data=roles.items, count=roles.total)
    return table_api(
        data=[{
            'id': Student.id,
            'name': Student.name,
        } for Student in roles.items],
        count=roles.total)
